//
//  WSSignCell.m
//  CharonTVProject
//
//  Created by karong on 2023/7/31.
//

#import "WSSignCell.h"
#import "WSSignItemCell.h"
#import "WSSign.h"
#import "WSADManager.h"

@interface WSSignCell () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UIView *bgView;

@property (weak, nonatomic) IBOutlet UIImageView *bgImage;
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) NSArray * dataArray;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *signBtn;
@property (weak, nonatomic) IBOutlet UILabel *myCoinsLabel;
@property (weak, nonatomic) IBOutlet UILabel *dayNumLab;

@property (nonatomic, strong) WSSignData * object;
@end

@implementation WSSignCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setMycoins) name:WSRefreshUserInfoNotification object:nil];
    self.bgView.layer.cornerRadius = 16;
    self.bgView.layer.borderWidth = 1;
    self.bgView.layer.borderColor = [UIColor colorFromHexRGB:@"FFFFFF"].CGColor;
    self.bgView.layer.shadowColor = [UIColor colorSameWithRGB:@"333333"].CGColor;
    self.bgView.layer.shadowRadius = 6;
    self.bgView.layer.shadowOpacity = 1;
    self.bgImage.layer.cornerRadius = 16;
    self.bgImage.layer.borderWidth = 1;
    self.bgImage.layer.borderColor = [UIColor colorFromHexRGB:@"FFFFFF"].CGColor;

}

- (void)setDataWithObject:(WSSignData *)object {
    
    self.object = object;
    self.dataArray = object.charonTV_items;
    if (object.charonTV_is_sign_today == 1 && [WSADManager shared].onlineTimePlayVideoToday) {
        [self.signBtn setTitle:APPLocalized(@"明天見") forState:(UIControlStateNormal)];
        self.signBtn.userInteractionEnabled = NO;
    }
    else if (object.charonTV_is_sign_today == 1) {
        [self.signBtn setTitle:APPLocalized(@"觀看視頻領雙倍積分") forState:(UIControlStateNormal)];
    }
    else {
        [self.signBtn setTitle:APPLocalized(@"簽到") forState:(UIControlStateNormal)];
    }
    [self.listView reloadData];
    [self layoutTitleLabelWithObject:object];
    [self setMycoins];
}

- (void)layoutTitleLabelWithObject:(WSSignData *)object {
    NSString *text = WSSF(@"%ld%@", (long)object.charonTV_sign_days,APPLocalized(@"天"));
    self.dayNumLab.text = text;
}

- (void)setMycoins {
    self.myCoinsLabel.text = WSSF(@"%ld", (long)appUserCenter.coins);
}

- (IBAction)signAction:(id)sender {
    if (self.object.charonTV_is_sign_today == 1) {
        [[WSADManager shared]loadRewardedAd];
    }
    else {
        if ([self.delegate respondsToSelector:@selector(signcharonTV_CellSignAction)]) {
            [self.delegate signcharonTV_CellSignAction];
        }
    }
    
}
- (IBAction)roleAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(signcharonTV_CellRoleAction)]) {
        [self.delegate signcharonTV_CellRoleAction];
    }
}

#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WSSignItemCell * cell;
    if (indexPath.row == self.dataArray.count - 1) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSSignItemLastDayCell" forIndexPath:indexPath];
    }
    else {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSSignItemCell" forIndexPath:indexPath];
    }
    [cell setDataWithObject:self.dataArray[indexPath.row] day:self.object.charonTV_sign_days];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    UIImage * img = WSIMG(@"img_qd");
    CGFloat width = floor((img.size.width - 18 - 18 - 10 * 3) / 4.0f);
    if (indexPath.row == self.dataArray.count - 1) {
        size.width = width * 2 + 10;
    }
    else {
        size.width = width;
    }
    size.height = 90;
    
    return size;
}

@end
